﻿using NetEasyMusic.Lib.Models;
using NetEasyMusic.Lib.Recommend;
using Newtonsoft.Json.Linq;

namespace NetEasyMusic.Lib.Data;

public class Personalized : DataBasic
{
    public Personalized(string cookie) : base(cookie)
    {
    }

    public Personalized()
    {
    }

    public Personalized(string url, bool isUse = false) : base(url, isUse)
    {
    }

    

    /// <summary>
    ///     推荐歌单
    /// </summary>
    /// <returns></returns>
    public async Task<PlaylistModel[]> Playlist()
    {
        var context = await GetFormString("/personalized");
        if (context == null) return [];
        var array = context["result"]!;
        return array.Select(x => new PlaylistModel(x)).ToArray();
    }

    public async Task<BannerModel[]> Banner()
    {
        var context = await GetFormString("/banner");
        if (context == null) return [];
        var array = context["banners"]!;
        return array.Select(x => new BannerModel()
        {
            ImageUrl = x["imageUrl"]?.ToObject<string>()!,
            Id = x["targetId"]?.ToObject<string>()!,
            Title = x["typeTitle"]?.ToObject<string>()!
        }).ToArray();
    }

    /// <summary>
    /// 推荐歌单(私人推荐)
    /// </summary>
    /// <returns></returns>
    public async Task<PlaylistModel[]> RecommendPlaylist()
    {
        var context = await GetFormString($"/recommend/resource?cookie={Cookie}");
        if (context == null) return [];
        return context["recommend"]?.Select(x => new PlaylistModel(x)).ToArray() ?? [];
    }

    // /recommend/songs
    public async Task<SongModel[]> RecommendSong()
    {
        var context = await GetFormString($"/recommend/songs?cookie={Cookie}");
        if (context == null) return [];
        var array = context["data"]?["dailySongs"]!;
        return array.Select(token =>
            new SongModel
            {
                Id = token["id"]!.ToObject<string>()!,
                Name = token["name"]!.ToObject<string>()!,
                PicUrl = token["al"]!["picUrl"]!.ToObject<string>()!,
                Artists = token["ar"]!.Select(x => new ArtistModel(x)).ToArray(),
                Time = token["dt"]!.ToObject<string>()!,
                Album = token["al"]!["name"]!.ToObject<string>()!,
                AlbumId = token["al"]!["id"]!.ToObject<string>()!
            }).ToArray();
    }

    /// <summary>
    ///     推荐新音乐
    /// </summary>
    /// <returns></returns>
    public async Task<SongModel[]> NewSong()
    {
        var context = await GetFormString("/personalized/newsong");
        if (context == null) return [];
        var array = (JArray)context["result"]!;
        return array.Select(token => new SongModel
        {
            Id = token["id"]?.ToObject<string>()!,
            Name = token["name"]?.ToObject<string>()!,
            PicUrl = token["picUrl"]?.ToObject<string>()!,
            Album = token["song"]?["album"]?["name"]?.ToObject<string>()!,
            AlbumId = token["song"]?["album"]?["id"]?.ToObject<string>()!,
            Time = token["song"]?["duration"]?.ToObject<string>()!,
            Artists = token["song"]?["artists"]?.Select(x => new ArtistModel(x)).ToArray() ?? []
        }).ToArray();
    }

    // 
    public async Task<SongModel[]> SongsRecommend()
    {
        var context = await GetFormString($"/recommend/songs?cookie={Cookie}");
        if (context == null) return [];
        var array = (JArray)context["data"]!["dailySongs"]!;
        return array.Select(token =>
        {
            var songRecommend = new SongModel
            {
                Id = token["id"]!.ToObject<string>()!,
                Name = token["name"]!.ToObject<string>()!,
                PicUrl = token["al"]!["picUrl"]!.ToObject<string>()!,
                Artists = ((JArray)token["ar"]!).Select(x => new ArtistModel(x)).ToArray(),
                Time = token["dt"]!.ToObject<string>()!,
                Album = token["al"]!["name"]!.ToObject<string>()!,
                AlbumId = token["al"]!["id"]!.ToObject<string>()!
            };
            return songRecommend;
        }).ToArray();
    }

    /// <summary>
    ///     推荐节目
    /// </summary>
    /// <returns></returns>
    public async Task<DJRecommend> Recommend()
    {
        var context = await GetFormString("/program/recommend");
        return new DJRecommend(context!);
    }

    /// <summary>
    ///     独家放送(入口列表)
    /// </summary>
    /// <returns></returns>
    public async Task<PrivateContentRecommend> PrivateContent()
    {
        var context = await GetFormString($"/personalized/privatecontent?cookie={Cookie}");

        return new PrivateContentRecommend(context!);
    }

    /// <summary>
    ///     独家放送列表
    /// </summary>
    /// <returns></returns>
    public async Task<PrivateContentItem[]> PrivateContentList()
    {
        var context = await GetFormString($"/personalized/privatecontent/list?cookie={Cookie}");
        if (context == null) return [];
        var array = (JArray)context["result"]!;
        return array.Select(x => new PrivateContentItem(x)).ToArray();
    }

    /// <summary>
    ///     私人FM
    /// </summary>
    /// <returns></returns>
    public async Task<SongModel[]> FM()
    {
        var context = await GetFormString($"/personal_fm?cookie={Cookie}");
        return context?["data"]!.Select(x => new SongModel
        {
            Album = x["album"]!["name"]!.ToObject<string>()!,
            AlbumId = x["album"]!["id"]!.ToObject<string>()!,
            Artists = x["artists"]!.Select(y => new ArtistModel(y)).ToArray(),
            Id = x["id"]!.ToObject<string>()!,
            Name = x["name"]!.ToObject<string>()!,
            PicUrl = x["album"]!["picUrl"]!.ToObject<string>()!,
            Time = x["duration"]!.ToObject<string>()!
        }).ToArray() ?? [];
    }
}